Skip to content

Batch tokenization in the transformers backend's scoring loop#1286

Open
ErenAta16 wants to merge 3 commits into
huggingface:mainfrom
ErenAta16:optimize-tokenization
Open

Batch tokenization in the transformers backend's scoring loop#1286
ErenAta16 wants to merge 3 commits into
huggingface:mainfrom
ErenAta16:optimize-tokenization

Conversation

@ErenAta16

Copy link
Copy Markdown

Closes #732.

tok_encode_pair tokenizes a context and its continuations with a separate tokenizer call per string, so scoring a mini-batch of documents issues one call per context plus one per continuation — on a large benchmark this turns into thousands of small Python-level calls.

Adds tok_encode_pair_batch, which does the same encoding (including the existing trailing-space handling) but with exactly two tokenizer calls per mini-batch: one for every context, one for every continuation across every document. Wires it into the transformers backend's _loglikelihood_tokens loop, which is the hot path the issue describes.

Verified that the batched output is identical to the old per-document calls across several add_special_tokens/move_trailing_context_space combinations, confirmed the existing end-to-end loglikelihood tests still pass against a real model, and added a test asserting the call count itself drops (2 calls per mini-batch instead of 2 per document).

ErenAta16 added 3 commits July 6, 2026 21:59
tok_encode_pair tokenizes a context and its continuations with a
separate tokenizer call per string, so a batch of documents ends up
issuing one call per context plus one per continuation. On large
benchmarks this turns into thousands of small Python-level calls.

Adds tok_encode_pair_batch, which does the same encoding (including
the trailing-space handling already covered by move_trailing_context_space)
but with exactly two tokenizer calls for the whole batch: one for every
context, one for every continuation across every document. This lets a
fast tokenizer batch and parallelize the work instead of paying
per-call overhead for each string.

Not wired into any backend yet; the next commit switches the
transformers backend's scoring loop over to it.
…tion

Replaces the per-document tok_encode_pair loop in _loglikelihood_tokens
with tok_encode_pair_batch, so each mini-batch tokenizes its contexts
and continuations with two tokenizer calls instead of one pair of
calls per document. Verified against test_loglikelihood_eval and
test_loglikelihood_padded_tensors_shapes, which exercise this path
end to end with a real model.
Guards the actual optimization, not just its correctness: asserts the
transformers backend's scoring loop calls _batch_tok_encode twice per
mini-batch (once for contexts, once for continuations) regardless of
how many documents or choices it contains, instead of once per
document. With 3 documents, a per-document loop would make 6 calls.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Optimize tokenization

1 participant